package com.ldg.shop.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ld.utils.PageUtils;
import com.ld.utils.Query;
import com.ldg.shop.dao.SpecificationDao;
import com.ldg.shop.dao.SpecificationOptionDao;
import com.ldg.shop.dao.TypeTemplateDao;
import com.ldg.shop.entity.SpecificationEntity;
import com.ldg.shop.entity.SpecificationOptionEntity;
import com.ldg.shop.entity.TypeTemplateEntity;
import com.ldg.shop.group.Specification;
import com.ldg.shop.service.SpecificationService;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("specificationService")
public class SpecificationServiceImpl extends ServiceImpl<SpecificationDao, SpecificationEntity> implements SpecificationService {

    @Resource
    private SpecificationOptionDao optionDao;
    @Resource
    private TypeTemplateDao templateDao;

    /**
     * 分页带条件查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SpecificationEntity> page = this.page(
                new Query<SpecificationEntity>().getPage(params),
                new QueryWrapper<SpecificationEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 2. 添加规格的方法
     * @param specification
     */
    @Override
    public void save(Specification specification) {
        //2.1 先添加规格选项
        SpecificationEntity spec = specification.getSpec();
        baseMapper.insert(spec);
        System.out.println("spec.getId() = " + spec.getId());
        //2.2 添加完成后就可以得到规格的id，将此id与规格选项绑定
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //2.2.1 设置规格选项的外键
            option.setSpecId(spec.getId());
            //2.2.2 开始添加规格选项
            optionDao.insert(option);
        }

    }

    /**
     * 3. 根据规格id查询出规格及规格选项列表
     * @param id
     * @return
     */
    @Override
    public Specification findById(String id) {
        //3.1 根据id查询出规格
        SpecificationEntity spec = baseMapper.selectById(id);
        //3.2 根据规格id查询出规格选项列表
        List<SpecificationOptionEntity> optiions = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", spec.getId()));
        //3.3 返回规格及规格选项列表的组合类
        return new Specification(spec,optiions);
    }

    //4. 修改规格及规格选项
    @Override
    public void update(Specification specification) {
        //4.1 先修改规格
        baseMapper.updateById(specification.getSpec());
        //4.2 再根据外键id（规格的主键值）来在规格选项表中删除原来的所有规格列表
        optionDao.delete(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",specification.getSpec().getId()));

        //4.3 再重新添加规格列表
        for (SpecificationOptionEntity option : specification.getOptions()) {
            //4.3.1 绑定与规格的id的外键关系
            option.setSpecId(specification.getSpec().getId());
            //4.3.2 添加规格
            optionDao.insert(option);
        }
    }

    /**
     * 5. 根据id批量删除
     * @param ids
     */
    @Override
    public void deleteAll(List<String> ids) {
        //5.1 删除规格选项
        for (String id : ids) {
            optionDao.delete(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id",id));
            //5.2 删除规格
            baseMapper.deleteById(id);
        }

    }

    /**
     * 6. 根据模板id查询规格及规格选项列表
     * @param id
     * @return
     */
    @Override
    public List<Map> findSpecList(String id) {
        //6.1 查询出模板对象,得到规格列表,转换为对象
        List<Map> specMap = JSON.parseArray(templateDao.selectById(id).getSpecIds(), Map.class);
        //6.2 遍历上面的集合
        for (Map map : specMap) {
            //6.2.1 得到规格id
            String specId = map.get("id").toString();
            //6.2.2 根据规格id找出规格选项列表
            List<SpecificationOptionEntity> options = optionDao.selectList(new QueryWrapper<SpecificationOptionEntity>().eq("spec_id", specId));
            //6.2.3 向map中放一个名为options的key
            map.put("options",options);
        }
        //6.3 返回
        return specMap;
    }

}
